﻿using CustomCharacter;
using CustomCharacter.Ability;
using CustomPlayerEffects;
using Exiled.API.Enums;
using Exiled.Events.EventArgs.Interfaces;
using Exiled.Events.EventArgs.Player;
using PlayerStatsSystem;
using Respawning;
using RgCore.Events;
using RgCore.GameData;
using System;
using System.Collections.Generic;
using System.Linq;
using CustomCharacter.Interfaces;
using UnityEngine;
using Player = Exiled.API.Features.Player;

namespace Scp076
{
    [RoleInfo("SCP-076", Side.Mtf, PlayerRoles.RoleTypeId.NtfSergeant, PlayerRoles.RoleTypeId.NtfSergeant)]
    public class Scp076 : CustomCharacter.CustomCharacter, IScpNumber, ITeamSpawn
    {
        public Scp076(int id) : base(id)
        {
        }

        public const float MaxAHP = 200f;

        public string ScpNumber => "076";

        public override Color32 RoleColor => new Color32(127, 255, 212, 255);

        public SpawnableTeamType SpawnType => SpawnableTeamType.NineTailedFox;

        public byte MaxWaves => 0;

        private static AbilityOutput Ability1(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            if (player.ArtificialHealth == 0)
                return new AbilityOutput(false, "你没有AHP！");
            player.Heal(player.ArtificialHealth * 0.6f);
            player.ArtificialHealth *= 0.4f;
            return new AbilityOutput(true);
        }

        private static AbilityOutput Attribute_1(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            if (e == null || trigger != AbilityTrigger.Event)
                return new AbilityOutput(false);
            if (e is HurtingEventArgs ev)
            {
                if (ev.IsAllowed && ev.DamageHandler.Base is AttackerDamageHandler)
                {
                    if (ev.Attacker == player)
                    {
                        if (!ev.Player.IsScp)
                        {
                            float newamount = ev.Attacker.ArtificialHealth + ev.Amount * 0.2f;
                            ev.Attacker.ArtificialHealth = Math.Min(newamount, MaxAHP);
                        }
                        ev.Player.EnableEffect<Burned>(0.5f, false);
                    }
                }
                return new AbilityOutput(true);
            }
            return new AbilityOutput(false);
        }

        public override void Role_Activate()
        {
            base.Role_Activate();
            Player.MaxArtificialHealth = MaxAHP;
        }

        public void OnTeamSpawn(List<Player> allPlayers)
        {
            List<Player> ava = allPlayers.Where(x => x.Role.Type == Attribute.OriginRole && !x.IsCustomCharacter() &&
  GameplayData.GameDatas.FirstOrDefault(y => y.Player == x)?.KeyBinded == true).ToList();
            if (ava.Count > 0)
            {
                Player tar = ava[UnityEngine.Random.Range(0, ava.Count)];
                SpawnPlayer(tar, GetType());
            }
        }

        public override IEnumerable<Ability> AbilityRegister()
        {
            return new Ability[]
            {
                new Ability("Scp076_1", "将当前 护盾  的 60% 用于恢复血量", AbilityType.Offensive, 60, Ability1, null, null, null, GetType()),
                new AttributeAbility("Scp076_Attr_1", "对人类造成伤害回盾(最大200)；攻击敌方造成0.5秒燃烧",
                Attribute_1, false, new IExiledEvent[] { EventProcessor.OnHurting }, null, GetType())
            };
        }
    }
}